package com.newway.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newway.common.context.UserInfoContext;
import com.newway.common.result.Result;
import com.newway.entity.Activity;
import com.newway.entity.ApplyRecord;
import com.newway.entity.ApplyRecordEntity;
import com.newway.entity.Member;
import com.newway.mapper.ActivityMapper;
import com.newway.mapper.ApplyRecordMapper;
import com.newway.mapper.MemberMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Resource
    ActivityMapper activityMapper;

    @Resource
    ApplyRecordMapper applyRecordMapper;

    @Resource
    MemberMapper memberMapper;


    /**
     * 展示可申请的活动
     * @return
     */
    @GetMapping("/getFrontList")
    public Result getFrontList(){
        QueryWrapper<Activity> activityQueryWrapper = new QueryWrapper<>();
        activityQueryWrapper.eq("status","1");
        List<Activity> activities = activityMapper.selectList(activityQueryWrapper);
        return Result.success(activities);
    }

    @GetMapping("/recordList")
    public Result getRecordList(@RequestParam(required = false,value = "status")String status){
        QueryWrapper<ApplyRecord> applyRecordQueryWrapper = new QueryWrapper<>();
        applyRecordQueryWrapper.eq("user_id", UserInfoContext.getUserInfo().getId());
        if(StringUtils.isNotBlank(status)){
            applyRecordQueryWrapper.eq("status",status);
        }
        applyRecordQueryWrapper.orderByDesc("id");
        List<ApplyRecord> applyRecords = applyRecordMapper.selectList(applyRecordQueryWrapper);
        return Result.success(applyRecords);
    }

    //取消申请
    @GetMapping("/cancel/{id}")
    public Result getRecordList(@PathVariable("id")Integer id){
        ApplyRecord applyRecord = applyRecordMapper.selectById(id);
        if(applyRecord!=null&&"0".equals(applyRecord.getStatus())){
            applyRecord.setStatus("3");
            applyRecordMapper.updateById(applyRecord);
        }
        return Result.success();
    }

    @PostMapping("/apply")
    public Result apply(@RequestBody ApplyRecord applyRecord){
        QueryWrapper<Member> memberQueryWrapper = new QueryWrapper<>();
        memberQueryWrapper.eq("user_id",UserInfoContext.getUserInfo().getId());
        memberQueryWrapper.eq("status","1");
        Member member = memberMapper.selectOne(memberQueryWrapper);
        if(member==null){
            return Result.fail("请先加入社团");
        }
        QueryWrapper<ApplyRecord> applyRecordQueryWrapper = new QueryWrapper<>();
        applyRecordQueryWrapper.eq("activity_id",applyRecord.getActivityId());
        applyRecordQueryWrapper.eq("user_id",UserInfoContext.getUserInfo().getId());
        applyRecordQueryWrapper.in("status", Arrays.asList("0","1"));
        ApplyRecord data = applyRecordMapper.selectOne(applyRecordQueryWrapper);
        if(data==null){
            applyRecord.setApplyTime(LocalDateTime.now());
            applyRecord.setStatus("0");
            applyRecord.setUserId(UserInfoContext.getUserInfo().getId());
            applyRecordMapper.insert(applyRecord);
            return Result.success();
        }
        if("0".equals(data.getStatus())){
            return Result.fail("请等待管理员审核");
        }
        if("1".equals(data.getStatus())){
            return Result.fail("无需重复申请");
        }
        applyRecord.setApplyTime(LocalDateTime.now());
        applyRecord.setStatus("0");
        applyRecord.setUserId(UserInfoContext.getUserInfo().getId());
        applyRecordMapper.insert(applyRecord);
        return Result.success();
    }


    /**
     * 服务端查询
     * @return
     */
    @GetMapping("/getList")
    public Result getList(Activity activity){
        Page<Activity> activityPage = new Page<>(activity.getPageNum(), activity.getPageSize());
        QueryWrapper<Activity> activityQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(activity.getStatus())){
            activityQueryWrapper.eq("status",activity.getStatus());
        }
        if(StringUtils.isNotBlank(activity.getTitle())){
            activityQueryWrapper.like("title",activity.getTitle());
        }
        Page<Activity> result = activityMapper.selectPage(activityPage, activityQueryWrapper);
        return Result.success(result);
    }


    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Activity activity){
        activityMapper.insertOrUpdate(activity);
        return Result.success();
    }

    @PostMapping("/delete/{id}")
    public Result addOrUpdate(@PathVariable("id")Integer id){
        activityMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/detail/{id}")
    public Result detail(@PathVariable("id")Integer id){
        Activity activity = activityMapper.selectById(id);
        return Result.success(activity);
    }

    @GetMapping("/changeStatus/{id}/{status}")
    public Result detail(@PathVariable("id")Integer id,@PathVariable("status")String status){
        Activity activity = activityMapper.selectById(id);
        if(activity!=null){
            activity.setStatus(status);
            activityMapper.updateById(activity);
        }
        return Result.success(activity);
    }

    @GetMapping("/getApplyList")
    public Result getApplyList(ApplyRecordEntity applyRecordEntity){
        List<ApplyRecordEntity> list= applyRecordMapper.getApplyList(applyRecordEntity);
        return Result.success(list);
    }

    @PostMapping("/approve")
    public Result approve(@RequestBody ApplyRecord applyRecord){
        ApplyRecord applyRecord1 = applyRecordMapper.selectById(applyRecord);
        if("1".equals(applyRecord1.getStatus())){
            applyRecord1.setStatus("1");
        }else{
            applyRecord1.setStatus("2");
            applyRecord1.setRemarks(applyRecord.getRemarks());
        }
        applyRecordMapper.updateById(applyRecord1);
        return Result.success();
    }
}

